/**
 * 
 */
package moreHealthModel.Student;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;

import moreHealthModel.Campus.CampusLocation;



/**
 * @author aloke
 *
 */

//Contains the Student Information and the Journey Data for the Student. 
public class StudentJourney extends CampusLocation
	{
		//ATTRIBUTES ======================================
		
		//Contains the Student Info
		public Student currentStudentx_;
		
		//The location where the student has been. 
		private ArrayList<CampusLocation> visitedPlacesx_ = new ArrayList<CampusLocation>();

	//TODO: Planning to add Paramaterized Constructor which will set the Student and the Location he traveled.
	
	//CONSTRUCTORS ======================================
	public StudentJourney(){}

	//INTENT: Stores the Student Information whose Journey we are tracking. 
	//PRE-CONDITION: 
		//1. Student Object cannot be null. Student Info should always be presesnt.
	//POST-CONDITION:
		//1. Sets the the Student Info whose journey is being tracked.
	public void setStudentInfo (Student currentStudent)
			{
				if (currentStudent!= null)
				{
				currentStudentx_= currentStudent;
				}
			}

	
	
	//INTENT: Calculates the Total Distance traveled by the student.
	//PRE-CONDITION :
		//1. Collection of location where the student has visited.
		//2. sj(Student Journey) object should have the latitude, longitude, scale and the Time Instances. 
		//3. The latitude, longitude and time should be greater than -1
		//4. The array List should contain only Student Journey objects.
	//POST-CONDITION:
		//Get the total horizontal and vertical distance if the slope of the path is 0 or infinity. 
		//Get the total angular distance if the slope of the path is 1.
		//Total distance traveled horizontally, vertically and angular with respect to the grid. 
	public  double TotalDistanceCalculator(ArrayList<CampusLocation> arrayList)
		{
			//The difference is the distance traveled from one grid to the next grid horizontally.
			double latitudediff=0.0;
			
			//The difference is the distance traveled from one grid to the next grid vertically.
			double longitudediff=0.0;
			
			//This slope tells whether the path is horizontal(180) or vertical(90)
			//or inclined(45).
			double slope=0.0;
			
			//Location coordinates of the Location in the Grid Map. 
			int latitude=0;
			int longitude=0;
			
			 //The total distance traveled by the Student horizontally and Vertically.
			double nonangleresult=0.0;
			
			//The total distance traveled by the Student in an inclined path.
			double angleresult=0.0;
			
			//The results are rounded off. 
			double roundOfEndResult= 0.0;
			
			//Total Distance traveled by the Student.
			double endresult=0.0;
			
			//Sets the Decimal format to the last two decimal places 
			//i.e. 0.00
			DecimalFormat  nf = new DecimalFormat("0.00");
			nf.setMaximumFractionDigits(2);
			nf.setMinimumFractionDigits(2); 
			
			//Get each Location Information the Student has been to. 
			Enumeration<CampusLocation> en = Collections.enumeration(arrayList);
			
			while (en.hasMoreElements())
			{
				//The Location Information where the Student was.
				CampusLocation sj = (CampusLocation) (en.nextElement());
				
				if (sj!=null && sj.getLocationLatitude()>-1 && sj.getLocationLatitude()>-1)
					{
				latitudediff = Math.abs(sj.getLocationLatitude()-latitude);
				longitudediff=Math.abs(sj.getLocationlongitude()-longitude);

				//Coordinates of the previous location visited
				latitude= sj.getLocationLatitude();
				longitude= sj.getLocationlongitude();
				
				slope = latitudediff/longitudediff;
				
				//According to y= mx +c if angle is 45 deg them m =1.
				//where m(Slope)
				if (longitudediff!=0 && slope==1)
				{
					angleresult= angleresult + (Math.hypot(getGridHeight(), getGridWidth()));
				}
					
				//if m=infinity or 0 then angle is 180 or 90 degree.
				else 
					{nonangleresult= nonangleresult + getGridHeight();}
			}
			
			//0.2 is subtracted to remove the extra distance added initially. 
			 endresult= nonangleresult + angleresult - 0.2;
			}

			//return the formatted result.
			String a = nf.format(endresult%11);			
			roundOfEndResult = Double.parseDouble(a);
			
			return roundOfEndResult;
			}

	
	//INTENT: Store the Location the Student has visited in an Array. 
	//PRE-CONDITION: 
		//1. Always Requires the Location Information where the Student visited.
	//POST-CONDITION:
		//1. Add the Location to the Array and store it for further references. 
	public void setStudentVisitedPlaces (CampusLocation locationObject)
		{
			if (locationObject!=null)
		{
			CampusLocation campusLocationObject = (CampusLocation) locationObject;
			
			if (campusLocationObject.getLocationLatitude()>-1 && campusLocationObject.getLocationlongitude()>-1)
			{	
			visitedPlacesx_.add(campusLocationObject);
			}
		}
			
	}
	
	//INTENT: Gets the places the Student has visited.
	//PRE-CONDITION:
		//1. The locations the Student visited should be stores in the array list. 
	//POST-CONDITION:
		//1. Return the all the Campus Location the Student has visited. 
	public ArrayList<CampusLocation> getStudentVisitedPlaces()
		{return visitedPlacesx_;}

	//@Override
	public ArrayList<String> CampusLocationDescription()
		{
			// TODO Auto-generated method stub
			return null;
		}
}

	
